package com.cykj.admin.service.impl;

import com.cykj.admin.mapper.LogMapper;
import com.cykj.admin.service.ILogService;
import com.cykj.admin.vo.LogVO;
import com.cykj.common.dto.ResponseDTO;
import com.cykj.common.entity.LogDO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: smart_recruit_backend
 * @BelongsPackage: com.cykj.admin.service.impl
 * @Author: 小小卢
 * @CreateTime: 2025-08-22 01:06
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class LogServiceImpl implements ILogService {

    @Autowired
    private LogMapper logMapper;

    /**
     * @Description: 查询日志
     * @Param: [logVO]
     * @return: com.cykj.common.dto.ResponseDTO
     */
    @Override
    public ResponseDTO getLogList(LogVO logVO) {
        PageInfo<LogDO> page;
        //开启分页
        PageHelper.startPage(logVO.getCurrentPage(), logVO.getPageSize());

        List<LogDO> logList = logMapper.getLogList(logVO);

        page = new PageInfo<>(logList);

        if (page.getTotal() < page.getPageSize() && logList.size() == 0 && page.getTotal() != 0) {
            logVO.setCurrentPage(1);
            PageHelper.startPage(logVO.getCurrentPage(), logVO.getPageSize());
            List<LogDO> logList2 = logMapper.getLogList(logVO);
            page = new PageInfo<>(logList2);
        }

        return ResponseDTO.success(page);
    }

    /**
    * @Description: 写入日志
    * @Param: [logVO]
    * @return: com.cykj.common.dto.ResponseDTO
    */
    @Override
    public ResponseDTO addLog(LogDO logDO) {
        Integer row = logMapper.addLog(logDO);
        if (row > 0){
            return ResponseDTO.success(row);
        }
        return ResponseDTO.error("日志插入失败");
    }
}